package com.example.myfirst.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.myfirst.dao.CategoryDao;
import com.example.myfirst.dao.ShopsDao;
import com.example.myfirst.entity.Category;
import com.example.myfirst.entity.Goods;
import com.example.myfirst.entity.Shops;
import com.example.myfirst.service.CategoryService;
import com.example.myfirst.service.GoodsService;
import com.example.myfirst.service.ShopsService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ShopsServiceImpl implements ShopsService {
    @Autowired
    private ShopsDao shopsDao;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private GoodsService goodsService;


    @ApiModelProperty("查询所有店铺信息")
    @Override
    public List<Shops> queryAllShops() {
        List<Shops> shopsList = shopsDao.selectList(null);
        for (Shops shops : shopsList) {
            List<Category> categoryList = categoryService.queryCategoryByShopId(shops.getId());
            List<Goods> list = new ArrayList<>();
            for (Category category : categoryList) {
                List<Goods> goods = goodsService.queryGoodsByCategoryId(category.getId());
                for (Goods good: goods) {
                    list.add(good);
                }
            }
            shops.setList(list);
        }
        return shopsList;
    }

    @Override
    public boolean addShop(Shops shops) {
        int i = shopsDao.insert(shops);
        return i > 0;
    }

    @Override
    public boolean delShopById(Integer id) {
        int i = shopsDao.deleteById(id);
        return i > 1;
    }

    // 同时根据店铺id获取到他的系列
    @Override
    public List<Shops> findShopById(Integer id) {
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("id", id);
        List<Shops> list = shopsDao.selectList(qw);
        for (Shops shops : list) {
            List<Category> categoryList = categoryService.queryCategoryByShopId(shops.getId());
            List<Goods> list1 = new ArrayList<>();
            for (Category category : categoryList) {
                List<Goods> goods = goodsService.queryGoodsByCategoryId(category.getId());
                for (Goods good : goods) {
                    list1.add(good);
                }
            }
            shops.setList(list1);
        }
        return list;
    }

    @Override
    public Boolean updateShopById(Shops shops, Integer id) {
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("id", id);
        int i = shopsDao.update(shops, qw);
        return i > 0;
    }
}
